<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/16
 * Time: 21:00
 */
namespace Pay\Controller;

use Common\Common\Api\Order;
use Common\Common\Api\Ordermember;
use Common\Common\Api\Redis\Channel;
use Common\Common\Api\Redis\Product;

/**
 * Class ManyiController
 * @package Pay\Controller
 * Gpay
 */
class GpayController extends PayController{
    protected $config;
    protected $class;
    public function __construct(){
        parent::__construct();
        $classes = explode("\\",__CLASS__);
        $class = end($classes);
        $this->class = substr($class, 0, stripos($class, "Controller"));
    }

    public function pay($array){
        $this->config = Channel::getInstance()->getById($array['channel']);

        if(!$this->config){
            $this->showmessage('通道错误!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        $parameter = array(
            'code' => $this->config['code'], // 通道名称
            'title' => $this->config['title'],
            'exchange' => 1, // 金额比例
            'gateway' => $this->config['gateway'],
            'orderid' => createOrderId(),
            'out_trade_id' => I("request.pay_orderid"),
            'body' => 'pay',
            'channel'=>$array
        );

        $isMobile = isMobile();
        $return = $this->orderadd( $parameter );

        switch($this->config['paytype']){
            //微信H5
            case 2:
                $pay_bankcode = '37';
                break;
            default:
                $this->showmessage('暂不支持此方式!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        $terminal = 1;//电脑端
        if($isMobile){
            $terminal = 2;//手机端
        }

        $native = array(
            "company_id" => $this->config['mch_id'],
            "bank_id" => '',
            "amount" => number_format($return['amount'],2,'.',''),
            "company_order_num" => $return['orderid'],
            "company_user" => $return['orderid'],
            "estimated_payment_bank" => '',
            "deposit_mode" => $pay_bankcode,
            "group_id" => 0,
            "web_url" => 'http://www.wanglianfu.com',
            "memo" => '',
            "note" => '',
            "note_model" => 2,
            "terminal" => $terminal,
        );

        $sign = md5(
            md5( $this->config['signkey']).
            $native['company_id'].
            $native['bank_id'].
            $native['amount'].
            $native['company_order_num'].
            $native['company_user'].
            $native['estimated_payment_bank'].
            $native['deposit_mode'].
            $native['group_id'].
            $native['web_url'].
            $native['memo'].
            $native['note'].
            $native['note_model']
        );
        $native["key"] = $sign;

        $res = curl($this->config['gateway'], httpstr($native));//curl_post

        $resp = json_decode($res,true);

        $code = $resp['break_url'];

        if(!$code){
            if($resp['error_msg']){
                $this->showmessage($resp['error_msg'],['code'=>C("THIRDCODES.".$this->class)]);
            }

            $this->showmessage('获取支付连接失败！',['code'=>C("THIRDCODES.".$this->class)]);
        }

        header("Location: {$code}");
    }

    //通知
    public function notifyurl(){
        $this->debug_callback();
        if(!$this->checkip(getIP(),['3.0.191.50'])){
            exit("wrong");
        }

        $p =  I('post.');

        if(!$p || empty($p)){
            exit("wrong");
        }

        //获取订单信息
        $orderid = $p['company_order_num'];
        $find = $this->checkOrderNotify($orderid);
        if(!$find){
            echo 'order_not_found'; exit;
        }

        //保存回调数据
        M("Order")->where(array("pay_orderid"=>$orderid))->save(array(

            "pay_orderid_rd"=> htmlspecialchars($p['mownecum_order_num'])
        ));

        //获取渠道信息
        $this->config = M("Channel")->where(array('code'=>$this->class))->find();

        $sign = $p['key'];

        $str = md5( $this->config['signkey']).
            $p['pay_time'].
            $p['bank_id'].
            $p['amount'].
            $p['company_order_num'].
            $p['mownecum_order_num'].
            $p['pay_card_num'].
            $p['pay_card_name'].
            $p['channel'].
            $p['area'].
            $p['fee'].
            $p['transaction_charge'].
            $p['deposit_mode'];

        $sign2 = md5($str);

        if($sign !== $sign2){
            exit("sign_wrong");
        }

        $return = [
            'company_order_num' =>$p['company_order_num'],
             'mownecum_order_num' =>$p['mownecum_order_num'],
             'status' =>'1',
             'error_msg' =>''
        ];
        $returnJson = json_encode($return);

        $this->async($find['pay_orderid'],$returnJson);
    }

    public function callBackUrl(){

        echo '处理成功'; exit;
    }
}
